<?php

namespace App\Controller\Api;

use App\Base\BaseController4Restful;
use App\Entity\Product;
use App\Repository\ProductRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class ApiProductController extends BaseController4Restful
{
    /**
     * @Route("/api/products", name="apiGetProducts",methods={"GET","POST"})
     */
    public function apiGetProducts(ProductRepository $repository,Request $request)
    {
        switch ($request->getMethod()) {
            case "POST":
                if (!$user = $this->getUser()) {return $this->json_out(203,"请登录后操作");}
                $data = $this->getRequestData();

                $data["cat1"] = "/api_/categories/".$data["cat1"];
                $data["cat2"] = "/api_/categories/".$data["cat2"];
                if (!is_array($data["pics"])) {
                    $data["pics"] = json_decode($data["pics"],true);
                }
                $data["cover"] = $data["pics"][0] ?? "";
                /**@var $Product Product*/
                $Product = $this->createEntity($data,Product::class);
                $Product->setUser($user)->setShop($user->getShop());
                $this->myEm()->persist($Product);$this->myEm()->flush();
                return $this->json_out(200,"创建成功", $Product);
            default:
                if (!$page = $this->getParamRequired("page")) {$page = 1;}
                $user = $this->getUser();
                $limit = 10;
                $offset = (int)abs($limit * ($page - 1));
                $total = $repository->count([]);
                $this->setMeta($page,$total,$limit);
                $list = $repository->findBy(array("user"=>$user),array("id"=>"DESC"),$limit,$offset);
                return $this->json_out(200,"获取成功", $list);
        }
    }

    /**
     * @Route("/api/products/{id}", name="apiAddProducts",methods={"GET","PUT","PATCH","DELETE"})
     */
    public function apiUpdateProducts(Product $Product,Request $request)
    {
        $data = $this->getRequestData();
        switch ($request->getMethod()) {
            case "DELETE":
                $this->myEm()->remove($Product);$this->myEm()->flush();
                return $this->json_out(200,"已移除");
            case "PATCH":
            case "PUT":

                $data["cat1"] = "/api_/categories/".$data["cat1"];
                $data["cat2"] = "/api_/categories/".$data["cat2"];
                if (!is_array($data["pics"])) {
                    $data["pics"] = json_decode($data["pics"],true);
                }
                $data["cover"] = $data["pics"][0] ?? "";
                /**@var $Product Product*/
                $Product = $this->updateEntity($data,Product::class,$Product);
                $this->myEm()->persist($Product);$this->myEm()->flush();
                break;
            case "Get":
            default:
        }
        $this->setGroups("product","home");
        return $this->json_out(200,"获取成功", $Product);
    }



    /**
     * @Route("/app/products/lists", name="apiProductsLists")
     */
    public function apiProductsLists(ProductRepository $repository)
    {
        $data = $this->getRequestData();
        if(!$page = $this->getParamRequired("page")){$page = 1;}
        $qb = $repository->createQueryBuilder("A");
        $list = [];
        $qb->select("A as product" );
        $qb->leftJoin("App:Shop","S","with","A.shop = S");

        $list = $qb->getQuery()->execute();

        $this->setGroups("product","home","shop");
//        $list=[];
        return $this->json_out(200,"获取成功", $list);
    }

}
